package com.magicsoft.app.db;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.database.sqlite.SQLiteException;
import android.util.Log;

import com.boyuan.huijiaoyu.R;

/**
 * 
 * @author Ender
 * 
 */
public class DataBaseCreateHelper {

	// The Android's default system path of your application database.
	private static String DATABASE_PATH = "/data/data/com.boyuan.huijiaoyu/databases/";
	public static String DATABASE_FILENAME = "colorholder.db";// 数据库文件名(每次更新字段必须改名字)
	private final Context myContext;

	public DataBaseCreateHelper(Context context) {

		this.myContext = context;
	}

	public void createDataBase() throws IOException {

		// 删除旧数据库(每次更新数据库字段，必须修改名字)
		// deleteOldDataBase();

		// 判断新数据库是否存在
		boolean dbExist = checkDataBase();

		if (dbExist) {
			Log.w("DB", "already have DataBase!");

		} else {
			try {
				Log.w("DB", "new DataBase!");
				copyDataBase();
			} catch (IOException e) {

				throw new Error("Error copying database");
			}
		}
	}

	/**
	 * 检查数据库是否存在
	 * 
	 * @return
	 */
	private boolean checkDataBase() {

		try {

			String databaseFilename = DATABASE_PATH + DATABASE_FILENAME;

			File file = new File(databaseFilename);
			if (file.exists()) {
				return true;
			}

		} catch (SQLiteException e) {
			e.printStackTrace();
		}

		return false;
	}

	/**
	 * 删除旧数据库V1
	 */
	private void deleteOldDataBase() {

		try {

			String databaseFilename = DATABASE_PATH + "colorholder.db";

			File file = new File(databaseFilename);
			if (file.exists()) {

				if (file.isFile()) {
					file.delete();
					Log.e("DB", "delete " + databaseFilename);
				}
				resetSyncUpdateTime(myContext);
			}

		} catch (SQLiteException e) {
			e.printStackTrace();
		}

	}

	/**
	 * //初始化同步时间，用于数据库改变后重新下载离线会员
	 * 
	 * @param context
	 */
	public static void resetSyncUpdateTime(Context context) {

		// SharePreferenceHelper.clearLastUpdateTime(context);

	}

	/**
	 * 复制数据库到安装目录
	 * 
	 * @throws IOException
	 */
	private void copyDataBase() throws IOException {

		// Admin admin = SharePreferenceHelper.GetAdmin(myContext);
		//
		// if (admin != null) {
		// SharePreferenceHelper.setLastUpdateTime(myContext, admin.getBid(),
		// "0");
		// }
		//
		String databaseFilename = DATABASE_PATH + DATABASE_FILENAME;
		File dir = new File(DATABASE_PATH);

		if (!dir.exists())
			dir.mkdir();

		FileOutputStream os = null;
		try {

			os = new FileOutputStream(databaseFilename);
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		}

		InputStream is = myContext.getResources().openRawResource(
				R.raw.colorholder);

		// InputStream is =null;
		byte[] buffer = new byte[8192];
		int count = 0;

		try {
			while ((count = is.read(buffer)) > 0) {
				os.write(buffer, 0, count);
				os.flush();
			}
		} catch (IOException e) {

			e.printStackTrace();
		}
		try {
			is.close();
			os.close();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}
}